package com.pricetag.pricetagandroid;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;

import com.pricetag.components.SearchHistoryListAdapter;
import com.pricetag.search.SearchHandler;
import com.pricetag.search.SearchRecord;

public class SearchHistoryListActivity extends FragmentActivity {

	private SearchHistoryListActivity me;
	private UpdateSearchHistoryListTask updateListTask;
	private AtomicBoolean isRunning = new AtomicBoolean(false);
	public AtomicLong updateInterval = new AtomicLong(800);
	private SearchHistoryListAdapter searchHistoryListAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		
		me = this;
		setContentView(R.layout.activity_search_history_list);		
	}

	@Override
	public void onStart() {
		super.onStart();
		ListView listView = (ListView) this.findViewById(R.id.search_history_list);
		SearchRecord[] searchRecords = SearchHandler.getSearchRecords();
		String[] dummyRecords = new String[searchRecords.length];
		searchHistoryListAdapter = new SearchHistoryListAdapter(this, dummyRecords, searchRecords);
		listView.setAdapter(searchHistoryListAdapter);

		this.updateInterval.set(800);
		this.isRunning.set(true);

		if (updateListTask != null) {
			updateListTask.cancel(true);
		}

		updateListTask = new UpdateSearchHistoryListTask();
		updateListTask.execute(null, null, null);
	}

	@Override
	public void onStop() {
		super.onStop();

		this.isRunning.set(false);

		if (updateListTask != null) {
			updateListTask.cancel(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_search_history_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_settings:
			switchToSettingsActivity();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void switchToSettingsActivity() {
		Intent intent = new Intent(this, SettingsActivity.class);
		this.startActivity(intent);
	}

	public void switchToCameraActivity() {
		Intent intent = new Intent(this, CameraActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
		this.startActivity(intent);
	}

	public void switchToCameraActivity(View view) {
		switchToCameraActivity();
	}

	private class UpdateSearchHistoryListTask extends AsyncTask<Void, Void, Void> {

		SearchRecord[] searchRecords;
		String[] dummyRecords;

		@Override
		protected Void doInBackground(Void... params) {
			try {
				Thread.sleep(updateInterval.get());
			} catch (Exception e) {
			}
			searchRecords = SearchHandler.getSearchRecords();
			dummyRecords = new String[searchRecords.length];
			return null;
		}

		@Override
		protected void onPostExecute(Void results) {
			if (updateInterval.get() < 1000) {
				ListView listView = (ListView) findViewById(R.id.search_history_list);
				searchHistoryListAdapter = new SearchHistoryListAdapter(me, dummyRecords, searchRecords);
				listView.setAdapter(searchHistoryListAdapter);
				updateInterval.set(1000);
			} else {
				searchHistoryListAdapter.notifyDataSetChanged();
			}
			if (isRunning.get()) {
				updateListTask = new UpdateSearchHistoryListTask();
				updateListTask.execute(null, null, null);
			}
		}
	}

	public void onDelete() {
		this.updateInterval.set(800);
	}

}
